package prepare
//
//import (
//	"io/ioutil"
//	"os"
//	"strings"
//
//	"cke.io/ckectl/app/cmd/options"
//	common "cke.io/ckectl/app/cmd/phases"
//	"cke.io/ckectl/app/constants"
//	"github.com/pkg/errors"
//	"k8s.io/klog"
//	kubeoptions "k8s.io/kubernetes/cmd/kubeadm/app/cmd/options"
//	"k8s.io/kubernetes/cmd/kubeadm/app/cmd/phases/workflow"
//	cmdutil "k8s.io/kubernetes/cmd/kubeadm/app/cmd/util"
//	utilsexec "k8s.io/utils/exec"
//)
//
//var (
//	secretPhaseExample = cmdutil.Examples(`
//		# Writes a dynamic environment file with kubelet flags from a InitConfiguration file.
//		kubeadm init phase kubelet-start --config config.yaml
//		`)
//)
//
//// NewKubeletStartPhase creates a kubeadm workflow phase that start kubelet on a node.
//func NewSecretPhase() workflow.Phase {
//	return workflow.Phase{
//		Name:    "secret",
//		Short:   "Write secret And make ssh localhost enabled",
//		Long:    "Write a file with KubeletConfiguration and an environment file with node specific kubelet settings, and then (re)start kubelet.",
//		Example: secretPhaseExample,
//		Run:     runSecret,
//		InheritFlags: []string{
//			options.LocalIp,
//			options.AllowKnowHosts,
//			kubeoptions.CfgPath,
//		},
//	}
//}
//
//// write key to localhost, make ssh localhost easy
//func runSecret(c workflow.RunData) error {
//	cfg, err := common.GetInitConfiguration(c)
//	if err!=nil {
//		return errors.New("can't get Init Configuration: " + err.Error())
//	}
//
//	// default value "127.0.0.1" for ansible localIp
//	if cfg.Ansible.LocalIp == "" {
//		cfg.Ansible.LocalIp = "127.0.0.1"
//		klog.V(1).Infof("[kubeadm-ansible] use default Ip %s", cfg.Ansible.LocalIp)
//	}
//
//	// new exec for key check and secret
//	exec := utilsexec.New()
//
//	// if err, maybe key if not exist
//	if exec.Command("test", "-f", constants.RootSshKeyName).Run() != nil {
//		err = exec.Command("ssh-keygen", "-f", constants.RootSshKeyName, "-N", "").Run()
//	}
//
//	// get publickey, and put into authorized_keys
//	key, err := exec.Command("cat", constants.RootSshPublicKeyName).CombinedOutput()
//
//	// if key dosn't exist, put into authorized_key
//	if out, err := ioutil.ReadFile(constants.RootSshAuthorizedKeyName); err!=nil || !strings.Contains(string(out), string(key)) {
//
//		if err = AppendFile(constants.RootSshAuthorizedKeyName, key); err != nil {
//			// do nothing
//		}
//	}
//	common.AddKnowHosts(append(cfg.Ansible.AllowKnowHosts, cfg.Ansible.LocalIp)...)
//
//	return common.WritePhaseDoneMsg( "prepare", "secret", cfg, nil)
//}
//
//// WriteCert writes the pem-encoded certificate data to certPath.
//// The certificate file will be created with file mode 0644.
//// If the certificate file already exists, it will be overwritten.
//// The parent directory of the certPath will be created as needed with file mode 0755.
//func AppendFile(file string, data []byte) error {
//	out, err := os.OpenFile(file, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0666)
//	if err != nil {
//		return common.HandleError(err)
//	}
//	defer out.Close()
//
//	return common.HandleError(out.Write(data))
//}
